package com.dgut.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.dgut.common.SelectRole;
import com.dgut.common.StateForUser;
import com.dgut.pojo.Administrator;
import com.dgut.pojo.receive.AdministratorAndBuyerReceive;
import com.dgut.pojo.receive.ReceiveId;
import com.dgut.service.AdministratorService;
import com.dgut.utils.PageRequestInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.ConcurrentHashMap;

@CrossOrigin
@Api("SuperAdminController")
@RestController
@RequestMapping("/superAdmin")
public class SuperAdminController {

    @Resource
    private AdministratorService administratorService;
    /**
     * start-hwf
     */

    /**
     * 返回状态正常的管理员列表
     * @param pageRequestInfo
     * @return
     */
    @SaCheckLogin
    @SaCheckRole(SelectRole.SUPER_ADMIN)
    @ApiOperation(value = "分页获取状态正常的管理员列表",notes = "")
    @PostMapping("/normalAdminPage")
    public Object normalAdminPage(@ApiParam(value = "分页信息",required = true) @RequestBody PageRequestInfo pageRequestInfo){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        /**
         * 需要超级管理员登录才有权限
         */
        resultMap.put("normalAdminPage",administratorService.normalAdminPage(pageRequestInfo));
        resultMap.put("result",true);
        resultMap.put("message","获取成功！");
        return resultMap;
    }

    /**
     * 返回状态为冻结的管理员列表
     * @param pageRequestInfo
     * @return
     */
    @SaCheckLogin
    @SaCheckRole(SelectRole.SUPER_ADMIN)
    @ApiOperation(value = "分页获取状态为冻结的管理员列表",notes = "")
    @PostMapping("/blockedAdminPage")
    public Object blockedAdminPage(@ApiParam(value = "分页信息",required = true) @RequestBody PageRequestInfo pageRequestInfo){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        /**
         * 需要超级管理员登录才有权限
         */
        resultMap.put("blockedAdminPage",administratorService.blockedAdminPage(pageRequestInfo));
        resultMap.put("result",true);
        resultMap.put("message","获取成功！");
        return resultMap;
    }

    /**
     * 返回所有的管理员列表
     * @param pageRequestInfo
     * @return
     */
    @SaCheckLogin
    @SaCheckRole(SelectRole.SUPER_ADMIN)
    @ApiOperation(value = "分页获取所有的管理员列表",notes = "")
    @PostMapping("/adminPage")
    public Object adminPage(@ApiParam(value = "分页信息",required = true) @RequestBody PageRequestInfo pageRequestInfo){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        /**
         * 需要超级管理员登录才有权限
         */
        resultMap.put("adminPage",administratorService.adminPage(pageRequestInfo));
        resultMap.put("message","获取成功！");
        resultMap.put("result",true);
        return resultMap;
    }

    /**
     * 获取管理员信息
     * @param receiveId
     * @return
     */
    @SaCheckLogin
    @SaCheckRole(SelectRole.SUPER_ADMIN)
    @ApiOperation(value = "根据ID获取管理员信息",notes = "")
    @PostMapping("/getAdmin")
    public Object getAdmin(@ApiParam(value = "管理员id",required = true) @RequestBody ReceiveId receiveId){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        /**
         * 需要超级管理员登录才有权限
         */
        Integer id = receiveId.getId();
        if(id != null){
            Administrator admin = administratorService.getAdmin(id);
            if(admin != null){
                resultMap.put("result",true);
                resultMap.put("admin",admin);
                resultMap.put("message","获取成功！");
            }else{
                resultMap.put("result",false);
                resultMap.put("message","获取失败！");
            }
        }else{
            resultMap.put("result",false);
            resultMap.put("message","获取失败！");
        }
        return resultMap;
    }

    /**
     * 添加管理员
     * @param administratorAndBuyerReceive
     * @return
     */
    @SaCheckLogin
    @SaCheckRole(SelectRole.SUPER_ADMIN)
    @ApiOperation(value = "添加管理员接口",notes = "")
    @PostMapping("/addAdmin")
    public Object addAdmin(@ApiParam(value = "管理员信息",required = true) @RequestBody AdministratorAndBuyerReceive administratorAndBuyerReceive) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();

        /**
         * 需要超级管理员登录才有权限注册管理员
         */
        if(administratorService.addAdmin(administratorAndBuyerReceive)){
            resultMap.put("result",true);
            resultMap.put("message","注册成功！");
        }else{
            resultMap.put("result",false);
            resultMap.put("message","注册失败！");
        }
        return resultMap;
    }


    /**
     * 更新管理员信息
     * @param administratorAndBuyerReceive
     * @return
     */
    @SaCheckLogin
    @SaCheckRole(SelectRole.SUPER_ADMIN)
    @ApiOperation(value = "修改管理员信息接口",notes = "")
    @PostMapping("/updateAdmin")
    public Object updateAdmin(@ApiParam(value = "管理员信息",required = true) @RequestBody AdministratorAndBuyerReceive administratorAndBuyerReceive) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();

        /**
         * 需要超级管理员登录才有权限注册管理员
         */
        if(administratorService.updateAdmin(administratorAndBuyerReceive)){
            resultMap.put("result",true);
            resultMap.put("message","修改成功！");
        }else{
            resultMap.put("result",false);
            resultMap.put("message","修改失败！");
        }
        return resultMap;
    }

    /**
     * 冻结管理员
     * @param receiveId
     * @return
     */
    @SaCheckLogin
    @SaCheckRole(SelectRole.SUPER_ADMIN)
    @ApiOperation(value = "冻结管理员接口",notes = "")
    @PostMapping("/blockAdmin")
    public Object blockAdmin(@ApiParam(value = "管理员id",required = true) @RequestBody ReceiveId receiveId) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();

        /**
         * 需要超级管理员登录才有权限注册管理员
         */
        Integer id = receiveId.getId();
        if(id != null){
            if(administratorService.changeAdminState(id, StateForUser.BLOCKED)){
                StpUtil.logoutByLoginId(id);
                resultMap.put("result",true);
                resultMap.put("message","冻结成功！");
            }
            else{
                resultMap.put("result",false);
                resultMap.put("message","冻结失败！");
            }
        }
        else{
            resultMap.put("result",false);
            resultMap.put("message","冻结失败！");
        }
        return resultMap;
    }

    /**
     * 解冻管理员
     * @param receiveId
     * @return
     */
    @SaCheckLogin
    @SaCheckRole(SelectRole.SUPER_ADMIN)
    @ApiOperation(value = "解冻管理员接口",notes = "")
    @PostMapping("/unblockAdmin")
    public Object unblockAdmin(@ApiParam(value = "管理员id",required = true) @RequestBody ReceiveId receiveId ) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();

        /**
         * 需要超级管理员登录才有权限注册管理员
         */
        Integer id = receiveId.getId();
        if(id != null){
            if(administratorService.changeAdminState(id, StateForUser.NORMAL)){
                resultMap.put("result",true);
                resultMap.put("message","解冻成功！");
            }
            else{
                resultMap.put("result",false);
                resultMap.put("message","解冻失败！");
            }
        }
        else{
            resultMap.put("result",false);
            resultMap.put("message","解冻失败！");
        }
        return resultMap;
    }

    /**
     * 删除管理员
     * @param receiveId
     * @return
     */
    @SaCheckLogin
    @SaCheckRole(SelectRole.SUPER_ADMIN)
    @ApiOperation(value = "删除管理员接口",notes = "")
    @PostMapping("/deleteAdmin")
    public Object deleteAdmin(@ApiParam(value = "管理员id",required = true) @RequestBody ReceiveId receiveId ) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();

        /**
         * 需要超级管理员登录才有权限注册管理员
         */
        Integer id = receiveId.getId();
        if(id != null){
            if(administratorService.deleteAdmin(id)){
                resultMap.put("result",true);
                resultMap.put("message","删除成功！");
            }
            else{
                resultMap.put("result",false);
                resultMap.put("message","删除失败！");
            }
        }
        else{
            resultMap.put("result",false);
            resultMap.put("message","删除失败！");
        }
        return resultMap;
    }

    /**
     * end-hwf
     */
}
